<template>
  <div class="Articles-container">
    <el-card shadow="never">
      <!-- 搜索栏 -->
      <el-row>
        <el-col :span="18">
          <el-form class="searchSubj" :data="searchForm">
            <el-form-item label="关键字" size="small">
              <el-input v-model="searchForm.keyword" />
            </el-form-item>
            <el-form-item label="状态" size="small">
              <el-select v-model="searchForm.state" placeholder="请选择">
                <el-option
                  v-for="item in articleState"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button size="small" @click="resetSearch()"> 清除 </el-button>
              <el-button
                size="small"
                type="primary"
                @click="getArticlesList(searchForm)"
              >
                搜索
              </el-button>
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="6" style="text-align: right">
          <el-button
            type="success"
            size="small"
            @click="dialogUpdataArticles('新增技巧')"
          >
            新增技巧
          </el-button>
        </el-col>
      </el-row>
      <!-- 数据提示 -->
      <el-alert
        v-if="alertText !== ''"
        :title="alertText"
        type="info"
        class="alert"
        :closable="false"
        show-icon
        cell-class-name="table_cell"
      ></el-alert>
      <!-- 表格数据 -->
      <el-table :data="ArticlesList" fit size="medium">
        <el-table-column label="序列" type="index"> </el-table-column>
        <el-table-column label="文章标题" prop="title" width="400">
          <template slot-scope="scope">
            <span>{{ scope.row.title }}</span>
            <span v-if="scope.row.videoURL != null" style="">
              <i class="el-icon-film" @click="videoShow(scope.row.videoURL)" />
            </span>
          </template>
        </el-table-column>
        <el-table-column lable="阅读数" prop="visits"> </el-table-column>
        <el-table-column label="录入者" prop="username"> </el-table-column>
        <el-table-column label="录入时间">
          <template slot-scope="scope">
            <span>{{ scope.row.createTime }}</span>
          </template>
        </el-table-column>
        <el-table-column label="状态">
          <template slot-scope="scope">
            <span>{{ scope.row.state === 1 ? "已启用" : "未启用" }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" class="rowButton" width="240">
          <!-- 操作按钮 -->
          <template slot-scope="scope">
            <el-button
              @click="checkViewgBtn(scope.row)"
              type="text"
              size="medium"
            >
              预览
            </el-button>
            <el-button
              type="text"
              size="medium"
              @click="checkChangeBtn(scope.row)"
            >
              {{ scope.row.state === 1 ? "禁用" : "启用" }}
            </el-button>
            <el-button
              type="text"
              size="medium"
              @click="checkUpdateBtn('修改文章', scope.row)"
              :disabled="scope.row.state === 0 ? false : true"
              >修改</el-button
            >
            <el-button
              type="text"
              size="medium"
              @click="deleteArticles(scope.row.id)"
              :disabled="scope.row.state === 0 ? false : true"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        style="margin-top: 20px; text-align: right"
        background
        :current-page="currentPage"
        :page-sizes="[5, 10, 20, 50]"
        :page-size="pageSize"
        layout="prev, pager, next, sizes, jumper"
        :total="total"
      >
      </el-pagination>
      <!-- 封装的新增修改对话框 -->
      <ArticlesAdd
        :title="dialogtitle"
        :flag="dialogVisible"
        :dialogobj="dialogobj"
        @confirm="handleConfirm"
      />
      <ArticlesPreview
        :flag="dialogVisible2"
        :previeContent="previewObj"
        @confirmPreview="handlePreview"
      />
      <div class="video-preview" v-if="videoPreview">
        <div class="close" @click="videoPreview = false">
          <span>
            <i class="el-icon-close"></i>
          </span>
        </div>
        <video :src="previewVideo" controls></video>
      </div>
    </el-card>
  </div>
</template>

<script>
import { parseTime } from '../../utils/index'
import {
  list,
  add,
  update,
  remove,
  changeState
} from '../../api/hmmm/articles'
import ArticlesAdd from '../components/articles-add.vue'
import ArticlesPreview from '../components/articles-preview.vue'
export default {
  components: {
    ArticlesAdd,
    ArticlesPreview
  },
  data () {
    return {
      searchForm: {
        keyword: '',
        state: null
      },
      currentPage: 1,
      pageSize: 10,
      ArticlesList: [],
      listLoading: true,
      searchArticlesName: '',
      total: 0,
      alertText: '',
      dialogtitle: '',
      dialogVisible: false,
      dialogVisible2: false,
      diaologSwitch: true,
      dialogobj: {},
      previewObj: {},
      labelPosition: 'right',
      dialogForm: {
        ArticlesName: '',
        visitis: 1,
        id: null
      },
      articleState: [
        {
          value: 1,
          label: '已启用'
        },
        {
          value: 0,
          label: '未启用'
        }
      ],
      videoPreview: false,
      previewVideo: ''
    }
  },
  created () {
    this.getArticlesList()
  },
  methods: {
    // 切换每页总数
    handleSizeChange (val) {
      //
      this.pageSize = val
      //
      this.getArticlesList()
    },
    // 切换当前页
    handleCurrentChange (val) {
      //
      this.currentPage = val
      this.getArticlesList()
    },
    // 获取文章列表数据
    async getArticlesList (obj = {}) {
      //
      const res = await list({
        page: this.currentPage,
        pagesize: this.pageSize,
        keyword: obj.keyword,
        state: obj.state
      })
      //
      res.data.items.forEach((item) => {
        item.createTime = parseTime(item.createTime)
      })
      this.ArticlesList = res.data.items
      this.total = res.data.counts
      this.alertText = `共 ${this.total} 条记录`
      console.log(this.ArticlesList)
    },
    // 显示对话框
    dialogUpdataArticles (title, obj = {}) {
      this.dialogtitle = title
      this.dialogForm.id = obj.id || null
      this.dialogForm.ArticlesName = obj.ArticlesName || null
      this.dialogForm.visitis = obj.visitis + ''
      this.dialogVisible = true
    },
    // 对话框传回
    handleConfirm () {
      this.dialogVisible = false
      this.getArticlesList()
    },
    // 预览对话框传回
    handlePreview () {
      //
      this.dialogVisible2 = false
    },
    // 提交
    async Submit () {
      if (this.dialogForm.id == null) {
        this.dialogForm.visitis = +this.dialogForm.visitis
        //
        //
        await add(this.dialogForm)
        this.dialogVisible = false
        this.getArticlesList()
      } else {
        this.dialogForm.visitis = +this.dialogForm.visitis
        //
        //
        await update(this.dialogForm)
        this.dialogVisible = false
        this.getArticlesList()
      }
    },
    // 重置搜索
    resetSearch () {
      this.searchForm = this.$options.data().searchForm
      this.getArticlesList()
    },
    // 点击预览标签按钮
    checkViewgBtn (obj) {
      //
      this.previewObj = obj
      this.dialogVisible2 = true
    },
    // 点击修改状态
    async checkChangeBtn (obj) {
      //
      await changeState({
        id: obj.id,
        state: obj.state === 1 ? 0 : 1
      })
      this.getArticlesList()
    },
    // 点击修改按钮
    checkUpdateBtn (title, obj) {
      this.dialogtitle = title
      //
      this.dialogobj = obj
      this.dialogVisible = true
    },
    // 删除学科消息框
    deleteArticles (id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          await remove({ id })
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.getArticlesList()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 视频预览
    videoShow (src) {
      this.videoPreview = true
      this.previewVideo = src
    }
  }
}
</script>

<style scoped lang="scss">
.Articles-container {
  padding: 0 10px;
  margin: 10px 0;
}

.searchSubj {
  display: flex;
  align-items: center;
  .el-form-item {
    display: flex;
    align-items: center;
    margin: 0 10px 18px 0;
  }
}
.rowButton {
  width: 100%;
  display: flex;
}
.el-alert {
  margin: 0 0 15px;
}

.table_cell .cell {
  white-space: nowrap;
}

.el-icon-film {
  color: #00f;
}

.video-preview {
  position: fixed;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.3);
  overflow: hidden;
  z-index: 9999;
  .close {
    width: 50px;
    height: 50px;
    position: absolute;
    top: 30px;
    left: 50%;
    -webkit-transform: translate(-50%);
    left: 50%;
    background: rgba(0, 0, 0, 0.4);
    -webkit-box-shadow: 0 0 5px rgba(0, 0, 0, 0.4);
    box-shadow: 0 0 5px rgba(0, 0, 0, 0.4);
    border-radius: 50%;
    text-align: center;
    line-height: 50px;
    color: #fff;
    font-size: 20px;
  }

}
 video {
    position: absolute;
    top: 150px;
    left: 50%;
    transform: translate(-50%);
  }
</style>
